package com.example.android60permission;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

/**
 * Created by Administrator on 2017/5/24.
 */

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    /**
     * 为子类提供一个权限检查的方法
     *
     * @param permissions
     * @return
     */
    protected boolean hasPermisson(String... permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
        return true;
    }


    /**
     * 为子类提供个权限请求方法
     *
     * @param code
     * @param permissions
     */
    protected void requestPermissionsCustom(int code, String... permissions) {
        ActivityCompat.requestPermissions(this, permissions, code);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 0:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doSdCardPermission();
                } else {
                    Log.e("error_sdcard", "sdCard被拒绝");
                }
                break;

            case 1:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    doCallPhonePermission();
                } else {
                    Log.e("error_callphoe", "callphone被拒绝");
                }
                break;
        }
    }

    /**
     * 默认的打电话权限处理
     */
    protected void doCallPhonePermission() {

    }

    /**
     * 默认的写SdCark权限处理
     */
    protected void doSdCardPermission() {

    }
}
